package com.landinfo.web.rest;

import com.codahale.metrics.annotation.Timed;
import com.landinfo.service.SampleLineMonitorService;
import com.landinfo.web.rest.util.HeaderUtil;
import com.landinfo.service.dto.SampleLineMonitorDTO;
import io.github.jhipster.web.util.ResponseUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.net.URI;
import java.net.URISyntaxException;

import java.util.List;
import java.util.Optional;

/**
 * REST controller for managing SampleLineMonitor.
 */
@RestController
@RequestMapping("/api")
public class SampleLineMonitorResource {

    private final Logger log = LoggerFactory.getLogger(SampleLineMonitorResource.class);

    private static final String ENTITY_NAME = "sampleLineMonitor";

    private final SampleLineMonitorService sampleLineMonitorService;

    public SampleLineMonitorResource(SampleLineMonitorService sampleLineMonitorService) {
        this.sampleLineMonitorService = sampleLineMonitorService;
    }

    /**
     * POST  /sample-line-monitors : Create a new sampleLineMonitor.
     *
     * @param sampleLineMonitorDTO the sampleLineMonitorDTO to create
     * @return the ResponseEntity with status 201 (Created) and with body the new sampleLineMonitorDTO, or with status 400 (Bad Request) if the sampleLineMonitor has already an ID
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PostMapping("/sample-line-monitors")
    @Timed
    public ResponseEntity<SampleLineMonitorDTO> createSampleLineMonitor(@RequestBody SampleLineMonitorDTO sampleLineMonitorDTO) throws URISyntaxException {
        log.debug("REST request to save SampleLineMonitor : {}", sampleLineMonitorDTO);
        if (sampleLineMonitorDTO.getId() != null) {
            return ResponseEntity.badRequest().headers(HeaderUtil.createFailureAlert(ENTITY_NAME, "error", "A new sampleLineMonitor cannot already have an ID")).body(null);
        }
        try {
            SampleLineMonitorDTO result = sampleLineMonitorService.save(sampleLineMonitorDTO);
            if(result == null){
                return ResponseEntity.badRequest()
                    .headers(HeaderUtil.createFailureAlert(ENTITY_NAME, "error", "保存失败！"))
                    .body(null);
            }
            return ResponseEntity.created(new URI("/api/sample-line-monitors/" + result.getId()))
                .headers(HeaderUtil.createEntityCreationAlert(ENTITY_NAME, result.getId().toString()))
                .body(result);
        } catch (Exception ex) {
            String message = ex.getLocalizedMessage();
            return ResponseEntity.badRequest()
                .headers(HeaderUtil.createFailureAlert(ENTITY_NAME, "error", message))
                .body(null);
        }
    }

    /**
     * PUT  /sample-line-monitors : Updates an existing sampleLineMonitor.
     *
     * @param sampleLineMonitorDTO the sampleLineMonitorDTO to update
     * @return the ResponseEntity with status 200 (OK) and with body the updated sampleLineMonitorDTO,
     * or with status 400 (Bad Request) if the sampleLineMonitorDTO is not valid,
     * or with status 500 (Internal Server Error) if the sampleLineMonitorDTO couldn't be updated
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PutMapping("/sample-line-monitors")
    @Timed
    public ResponseEntity<SampleLineMonitorDTO> updateSampleLineMonitor(@RequestBody SampleLineMonitorDTO sampleLineMonitorDTO) throws URISyntaxException {
        log.debug("REST request to update SampleLineMonitor : {}", sampleLineMonitorDTO);
        if (sampleLineMonitorDTO.getId() == null) {
            return createSampleLineMonitor(sampleLineMonitorDTO);
        }
        SampleLineMonitorDTO result = sampleLineMonitorService.save(sampleLineMonitorDTO);
        return ResponseEntity.ok()
            .headers(HeaderUtil.createEntityUpdateAlert(ENTITY_NAME, sampleLineMonitorDTO.getId().toString()))
            .body(result);
    }

    /**
     * GET  /sample-line-monitors : get all the sampleLineMonitors.
     *
     * @return the ResponseEntity with status 200 (OK) and the list of sampleLineMonitors in body
     */
    @GetMapping("/sample-line-monitors")
    @Timed
    public List<SampleLineMonitorDTO> getAllSampleLineMonitors() {
        log.debug("REST request to get all SampleLineMonitors");
        return sampleLineMonitorService.findAll();
    }

    /**
     * GET  /sample-line-monitors : get all the sampleLineMonitors.
     *
     * @return the ResponseEntity with status 200 (OK) and the list of sampleLineMonitors in body
     */
    @GetMapping("/sample-line-monitors/all/{userName}")
    @Timed
    public List<SampleLineMonitorDTO> getAllSampleLineMonitorsByUser(@PathVariable String userName) {
        log.debug("REST request to get all SampleLineMonitors by userName");
        return sampleLineMonitorService.getAllSampleLineMonitorsByUser(userName);
    }

    /**
     * GET  /sample-line-monitors/:id : get the "id" sampleLineMonitor.
     *
     * @param id the id of the sampleLineMonitorDTO to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the sampleLineMonitorDTO, or with status 404 (Not Found)
     */
    @GetMapping("/sample-line-monitors/{id}")
    @Timed
    public ResponseEntity<SampleLineMonitorDTO> getSampleLineMonitor(@PathVariable Long id) {
        log.debug("REST request to get SampleLineMonitor : {}", id);
        SampleLineMonitorDTO sampleLineMonitorDTO = sampleLineMonitorService.findOne(id);
        return ResponseUtil.wrapOrNotFound(Optional.ofNullable(sampleLineMonitorDTO));
    }

    /**
     * DELETE  /sample-line-monitors/:id : delete the "id" sampleLineMonitor.
     *
     * @param id the id of the sampleLineMonitorDTO to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @DeleteMapping("/sample-line-monitors/{id}")
    @Timed
    public ResponseEntity<Void> deleteSampleLineMonitor(@PathVariable Long id) {
        log.debug("REST request to delete SampleLineMonitor : {}", id);
        sampleLineMonitorService.delete(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(ENTITY_NAME, id.toString())).build();
    }
}
